import {
    judgeOs,
    arrayMove,
    getCurrAbsPath,
    rltToAbs,
    arrayEqual,
    arrayDiff,
    OlFunction,
    inheritClass,
    Delegate,
    encodeHTML,
    decodeHTML,
    templateStringRender,
    strToVar,
    Hashcaller,
    HashListener,
    hashcaller,
    requestAPI,
    download
} from "../js/basics/Basics.js";
import {
    stopPropagation,
    stopEvent,
    stopPE,
    nodeListToArray,
    KeyNotbook,
    addKeyEvent,
    removeKeyEvent,
    addResizeEvent,
    linkClick,
    setupLinkClick
} from "../js/basics/dom_tool.js";
import {
    getBezierMatrix,
    get_BezierCurvePoint__DeCasteljau,
    matrixMULT,
    calcPascalsTriangle,
    getBezierCoefficient,
    get_BezierDerivativesPoints,
    createCutBezierMatrixQ,
    cutBezierByMatrix,
    binaryLinearEquation,
    approximately,
    derivative,
    rootsOfCubic,
    coefficientToPoints,
    Stepper,
    deg
} from "../js/basics/math_ex.js";

import{
    DEF_VirtualElementList,
    DEF_VirtualElement,
    DEF_CSSVE,
    DEF_CSSVEItem,
    DataLink,
    AttrKeyStrCtrl,
    AttrKeyStrCtrl__Ex,
    AttrKeyStrCtrlList,
    CtrlLib,
    ExCtrl
} from "../js/ControlLib/CtrlLib.js"

window.rltToAbs=rltToAbs;
function getPageCtrl(exCtrl_CallBack,_exCtrl){
    getPageCtrl.test1;
    if(!getPageCtrl.i){
        getPageCtrl.i+=1;
        var cssTag=document.createElement("link");
        cssTag.setAttribute("rel","stylesheet");
        cssTag.setAttribute("href",rltToAbs("./page.css",getPageCtrl.url));
        document.head.appendChild(cssTag);
        
        var EXCtrl_BluePrintXml_request=new XMLHttpRequest();
        EXCtrl_BluePrintXml_request.open("get",rltToAbs("./page.xml",getPageCtrl.url));

        EXCtrl_BluePrintXml_request.onload=function(e){
            var BluePrintXmlList=this.response.split("<ctrl_tab/>");

            class PageCtrlBase extends ExCtrl{
                constructor(data){
                    super(data);
                    this.apiUrls={
                        headlines:"./testdata/headlinesAPI.json",
                        headlinesMate:"./testdata/contentData3.json",
                        hello:[
                            "./testdata/contentData1.json",
                            "./testdata/contentData1.json",
                            "./testdata/contentData1.json"
                        ]
                    }
                }
                callback(){
                    addResizeEvent.reResize(this.elements.root);
                }
            }
            PageCtrlBase.prototype.childCtrlType=_exCtrl;

            class PageHome extends PageCtrlBase{
                constructor(data){
                    super(data);
                    this.img_list=[];
                }
                init_ImgList(){
                    var that=this;
                    console.log(1)
                    requestAPI("get",rltToAbs("testdata/contentData1.json",location.origin+'/'),function(e,e1){
                        that.img_list=JSON.parse(this.response).list;
                        that.reRender();
                    });
                    var temp={};
                    Object.defineProperty(temp,"list",{
                        get() { return that.img_list; },
                      });
                    console.log(temp.list);
                    return [temp];
                }
            }
            class Select extends PageCtrlBase{
                constructor(data){
                    super(data);
                    /** 用于找到api的属性, 这个值应该截取自 location.hash */
                    this.keyStr="";
                    this.pageindex=0;
                    this.loadSelectStr();
                    this.callChild("contentPageTab",function(){
                        this.tgtCtrlID="readingList";
                    })
                    this.ctrlActionList["render"]=[];
                    this.ctrlActionList["render"].push(function(){
                        this.loadSelectStr();
                    });
                }
                /** 重新读取 location.hash 的 信息
                 */
                loadSelectStr(){
                    var temp=Select.pageAttributeKey.exec(location.hash);
                    this.keyStr=temp[1];
                    
                    this.apiUrl=Select.selectApis[this.keyStr.split('/')[0]];
                    this.pageindex=parseInt(temp[3]);
                    if(isNaN(this.pageindex))this.pageindex=0;
                    var that=this;
                    this.callChild("contentPageTab",function(){
                        this.pageBase=that.keyStr;
                        this.reRender();
                    })
                }
            }
            Select.selectApis={
                    bbs         :rltToAbs("/testdata/readingList.json",location.origin),
                    blog        :rltToAbs("/testdata/readingList.json",location.origin),
                    select      :rltToAbs("/testdata/readingList.json",location.origin)
                }
            class BBsPage extends PageCtrlBase{
                constructor(data){
                    super(data);
                }
            }
            class BlogPage extends PageCtrlBase{
                constructor(data){
                    super(data);
                }
            }
            class Page404 extends PageCtrlBase{
                constructor(data){
                    super(data);
                }
            }
            PageHome.prototype.bluePrint    =DEF_VirtualElementList.xmlToVE(BluePrintXmlList[0]);
            Select.prototype.bluePrint      =DEF_VirtualElementList.xmlToVE(BluePrintXmlList[1]);
            BBsPage.prototype.bluePrint     =DEF_VirtualElementList.xmlToVE(BluePrintXmlList[2]);
            BlogPage.prototype.bluePrint    =DEF_VirtualElementList.xmlToVE(BluePrintXmlList[3]);
            Page404.prototype.bluePrint     =DEF_VirtualElementList.xmlToVE(BluePrintXmlList[4]);
            getPageCtrl.home    =PageHome;
            getPageCtrl.select  =Select;
            getPageCtrl.bbsPage =BBsPage;
            getPageCtrl.blogPage=BlogPage;
            getPageCtrl.page404=Page404;
            exCtrl_CallBack({
                home    :getPageCtrl.home,
                select  :getPageCtrl.select,
                bbs     :getPageCtrl.bbsPage,
                blog    :getPageCtrl.blogPage,
                page404 :getPageCtrl.page404,
            });
        }
        EXCtrl_BluePrintXml_request.send();
    }
}
getPageCtrl.i=0;
getPageCtrl.url=getCurrAbsPath();

export{
    getPageCtrl
}